package cn.agriculture.web.service;

import java.sql.SQLException;
import java.util.List;

import jp.terasoluna.fw.dao.QueryDAO;
import jp.terasoluna.fw.dao.UpdateDAO;
import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import cn.agriculture.web.form.CommodityForm;

@Service
public class CommodityService {
	@Autowired
	QueryDAO queryDao;
	
	@Autowired
	UpdateDAO updateDao;
	
	@Autowired
	Cache pictureCache;
	
	public List<CommodityForm> searchCommodityList() {
		List<CommodityForm> result = queryDao.executeForObjectList("Commodity.selectCommodityList", null);
		return result;
	}
	
	public CommodityForm searchCommodity(CommodityForm frm) {
		CommodityForm result = queryDao.executeForObject("Commodity.selectCommodity", frm, CommodityForm.class);
		return result;
	}
	
	public boolean addCommodity(CommodityForm frm) {
		Integer sequee = queryDao.executeForObject("Commodity.getSeq", null, Integer.class);
		String commodityId = frm.getUpdateTime().substring(0, 4) + String.format("%011d", sequee);
		frm.setCommodityId(commodityId);
		if (frm.getPicture().length != 0) {
			frm.setPictureId(commodityId);
			int picResult = updateDao.execute("Commodity.addPicture", frm);
			if (picResult != 1) {
				return false;
			}
		}
		int result = updateDao.execute("Commodity.addCommodity", frm);
		if (result == 1) {
			return true;
		}
		
		return false;
	}
	
	public boolean editCommodity(CommodityForm frm) {
		if (frm.getPicture().length != 0) {
			int picResult;
			if (StringUtils.isEmpty(frm.getPictureId())) {
				Integer sequee = queryDao.executeForObject("Commodity.getSeq", null, Integer.class);
				String pictureId = frm.getUpdateTime().substring(0, 4) + String.format("%011d", sequee);
				frm.setPictureId(pictureId);
				picResult = updateDao.execute("Commodity.addPicture", frm);
			} else {
				picResult = updateDao.execute("Commodity.editPicture", frm);
			}
			
			if (picResult != 1) {
				return false;
			}
		}
		int result = updateDao.execute("Commodity.editCommodity", frm);
		if (result == 1) {
			pictureCache.remove(frm.getPictureId());
			pictureCache.flush();
			return true;
		}
		return false;
	}
	
	public boolean delCommodity(CommodityForm frm) {
		if (!StringUtils.isEmpty(frm.getPictureId())) {
			int picResult = updateDao.execute("Commodity.delPicture", frm);
			if (picResult != 1) {
				return false;
			}
		}
		updateDao.execute("Commodity.delStock", frm);
		int result = updateDao.execute("Commodity.delCommodity", frm);
		if (result == 1) {
			return true;
		}
		return false;
	}
	
	public byte[] getPicture(CommodityForm frm) throws SQLException {
		if (pictureCache.get(frm.getPictureId()) == null) {
			frm = queryDao.executeForObject("Commodity.selectPicture", frm, CommodityForm.class);
			if (frm != null) {
				Element element = new Element(frm.getPictureId(), frm.getPicture());
				pictureCache.put(element);
				pictureCache.flush();
				System.out.println("从DB取得图片");
				return frm.getPicture();
			}
			return null;
		} else {
			System.out.println("从缓存取得图片");
			return (byte[])pictureCache.get(frm.getPictureId()).getObjectValue();
		}
		
		
		
	}
}
